/*
* $Id: InspectTable.cpp 54 2010-03-09 20:16:01Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "InspectTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CInspectTable::CInspectTable()
: CTable(INSPECT_TABLE_NAME){}
CInspectTable::~CInspectTable(){}

void CInspectTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
							_T("(id_journal INTEGER NOT NULL,")\
							_T("id_housing INTEGER NOT NULL,")\
							_T("honey INTEGER NULL,")\
							_T("brood INTEGER NULL,")\
							_T("open_brood INTEGER NULL,")\
							_T("close_brood INTEGER NULL,")\
							_T("goods_honey INTEGER NULL,")\
							_T("CONSTRAINT fkJournalJournal")\
							_T("	FOREIGN KEY(id_journal)")\
							_T("	REFERENCES journal(id_journal));"));
}
void CInspectTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET honey=?, brood=?, open_brood=?, close_brood=?, goods_honey=?")\
					_T(" WHERE id_journal=? AND id_housing=?;"));
	binder.SetValue(6, m_content.s_nIdHousing);
	binder.SetValue(2, m_content.s_nOpenBrood);
	binder.SetValue(5, m_content.s_nIdJournal);
	binder.SetValue(0, m_content.s_nHoney);
	binder.SetValue(3, m_content.s_nCloseBrood);
	binder.SetValue(1, m_content.s_nBrood);
	binder.SetValue(4, m_content.s_nGoodsHoney);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CInspectTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_nIdJournal != -1){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_journal=?"));
		binder.SetValue(0, m_content.s_nIdJournal);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CInspectTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + 
					_T("(id_journal, id_housing, honey, brood, open_brood, close_brood, goods_honey)")\
					_T(" VALUES(?, ?, ?, ?, ?, ?, ?);"));
	binder.SetValue(0, m_content.s_nIdJournal);
	binder.SetValue(1, m_content.s_nIdHousing);
	binder.SetValue(2, m_content.s_nHoney);
	binder.SetValue(3, m_content.s_nBrood);
	binder.SetValue(4, m_content.s_nOpenBrood);
	binder.SetValue(5, m_content.s_nCloseBrood);
	binder.SetValue(6, m_content.s_nGoodsHoney);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
void CInspectTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_nIdJournal){
		strRequest = strRequest + GetNameTable() + _T(" WHERE id_journal=?");
		binder.SetValue(0, m_content.s_nIdJournal);
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CInspectTable::CreateContent(){
	return new content;
}

void CInspectTable::content::Clear(){
	s_nId = 0;
	s_nIdJournal = -1;
	s_nIdHousing = 0;
	s_nHoney = 0;
	s_nBrood = 0;
	s_nOpenBrood = 0;
	s_nCloseBrood = 0;
	s_nGoodsHoney = 0;
}
CInspectTable::content::content(){
	Clear();
}
void CInspectTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_journal"))
			s_nIdJournal = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_housing"))
			s_nIdHousing = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("honey"))
			s_nHoney = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("brood"))
			s_nBrood = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("open_brood"))
			s_nOpenBrood = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("close_brood"))
			s_nCloseBrood = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("goods_honey"))
			s_nGoodsHoney = _ttoi(it->m_strValueColumn.c_str());
	}
}
void CInspectTable::content::Free(){
	delete this;
}